package com.wayhome.srpingbootmybatis.async.service.impl;

import com.alibaba.fastjson.JSON;
import com.wayhome.srpingbootmybatis.async.dao.AsyncTaskMessageMapper;
import com.wayhome.srpingbootmybatis.async.dto.AsyncTaskAddDTO;
import com.wayhome.srpingbootmybatis.async.dto.AsyncTaskMessage;
import com.wayhome.srpingbootmybatis.async.dto.AsyncTaskQueryDTO;
import com.wayhome.srpingbootmybatis.async.service.AsyncTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AsyncTaskServiceImpl implements AsyncTaskService {

    @Autowired
    private AsyncTaskMessageMapper taskMessageMapper;

    @Override
    public AsyncTaskMessage saveAsyncTask(AsyncTaskAddDTO asyncTaskDTO) {
        AsyncTaskMessage asyncTaskMessage = new AsyncTaskMessage();
        Date now = new Date();
        asyncTaskMessage.setTaskCreateTime(new Date());
        asyncTaskMessage.setOptTime(now);
        asyncTaskMessage.setTaskKey(asyncTaskDTO.getTaskKey());
        asyncTaskMessage.setTaskDesc(asyncTaskDTO.getTaskDesc());
        asyncTaskMessage.setTaskType(asyncTaskDTO.getTaskType());
        asyncTaskMessage.setTaskStatus((byte)0);
        asyncTaskMessage.setBizParams(JSON.toJSONString(asyncTaskDTO.getJobParameter()));
        taskMessageMapper.insertSelective(asyncTaskMessage);
        return asyncTaskMessage;
    }

    @Override
    public int queryUserRunningTasks(AsyncTaskQueryDTO asyncTaskQueryDTO) {
        return 0;
    }

    @Override
    public Integer queryForExist(AsyncTaskQueryDTO asyncTaskQueryDTO) {
        return null;
    }
}
